﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommerceOne.Common;
using CommerceOne.Controllers;
using CommerceOne.Domains.IRepositories;
using CommerceOne.Domains.SqlRepositories;
using CommerceOne.Models.Entities;

namespace CommerceOne.Widgets.Breadcrumb
{
    public class BreadcrumbController : ControllerBase
    {
        private readonly IProductCategoryRepository _categoryRepo = SqlProductCategoryRepository.Instance;
        private readonly IProductRepository _productRepo = SqlProductRepository.Instance;

        public BreadcrumbViewModel GetViewModel (Parameters parameter)
        {
            string configPath = @"~\Configurations\Breadcrumb.xml";
            configPath = this.HttpContextBase.Server.MapPath(configPath);
            var configuration = SerializeService.Instance.DeserializeObject<Configurations>(configPath);

            var items = configuration.Breadcrums.FirstOrDefault(p => p.PageType.Equals(parameter.PageType, StringComparison.OrdinalIgnoreCase));
            if (items == null) throw new Exception("There is no pagetype [" + parameter.PageType + "] in the config file [" + configPath +"]");

            var viewModel = new BreadcrumbViewModel{ BreadCrumbList = new List<BreadcrumbItem>()};
            viewModel.BreadCrumbList = new List<BreadcrumbItem>(); 
            items.BreadcrumbItem = items.BreadcrumbItem.OrderBy(p => p.No).ToList();

            switch (parameter.PageType)
            {
                case PageType.ProductList:
                    foreach (var breadcrumbItem in  items.BreadcrumbItem)
                    {
                        if (breadcrumbItem.Content.Equals("{" + VariableItem.Category + "}", StringComparison.OrdinalIgnoreCase) && parameter.ObjectId.HasValue)
                        {
                            List<BreadcrumbItem> list = GetCategoryItems(parameter.ObjectId.Value, breadcrumbItem.Clickable);
                            foreach (var item in list)
                            {
                                item.No = viewModel.BreadCrumbList.Count;
                                viewModel.BreadCrumbList.Add(item);
                            }
                        }
                        else
                        {
                            breadcrumbItem.No = viewModel.BreadCrumbList.Count;
                            viewModel.BreadCrumbList.Add(breadcrumbItem);
                        }
                    }
                    break;

                case PageType.ProductDetail:
                    if (!parameter.ObjectId.HasValue) break;
                     int categoryId = 0;
                    Product product = this._productRepo.GetProductById(parameter.ObjectId.Value);
                    foreach (var breadcrumbItem in items.BreadcrumbItem)
                    {
                        if (breadcrumbItem.Content.Equals("{" + VariableItem.Product + "}", StringComparison.OrdinalIgnoreCase) && product != null)
                        {
                            breadcrumbItem.No = viewModel.BreadCrumbList.Count;
                            breadcrumbItem.Content = product.Name;
                            if (breadcrumbItem.Clickable && !String.IsNullOrEmpty(breadcrumbItem.Url))
                                breadcrumbItem.Url = UrlBuilderService.Instance.GetUrlRoute(PageType.ProductDetail, product.ID, 0, string.Empty, string.Empty);
                            viewModel.BreadCrumbList.Add(breadcrumbItem);
                        }
                        else if (breadcrumbItem.Content.Equals("{" + VariableItem.Category + "}", StringComparison.OrdinalIgnoreCase) && product != null && product.SubCategoryID.HasValue)
                        {
                            var list = GetCategoryItems(product.SubCategoryID.Value, breadcrumbItem.Clickable);
                            foreach (var item in list)
                            {
                                item.No = viewModel.BreadCrumbList.Count;
                                viewModel.BreadCrumbList.Add(item);
                            }
                        }
                        else
                        {
                            breadcrumbItem.No = viewModel.BreadCrumbList.Count;
                            viewModel.BreadCrumbList.Add(breadcrumbItem);
                        }
                       
                    }
                    break;
                default:
                    break;
            }
           

            return viewModel;
        }

        public List<BreadcrumbItem> GetCategoryItems (int id, bool isClickable)
        {
            var items = new List<BreadcrumbItem>();
            ProductCategory category = this._categoryRepo.GetCategoryWithId(id);
            while (category != null)
            {
                var item = new BreadcrumbItem {Content = category.Name};
                string url = UrlBuilderService.Instance.GetUrlRoute(PageType.ProductList, category.ProductCategoryID, 1, category.Name, string.Empty);
                item.Url = url;
                item.Clickable = true;
                items.Insert(0, item);
                if (!category.ParentID.HasValue) category = null;
                else
                {
                    category = this._categoryRepo.GetCategoryWithId(category.ParentID.Value);
                }
            }

            if (!isClickable && items.Count > 0) items[items.Count - 1].Clickable = false;
            return items;
        }
    }
}